package com.compose.ui.refresh

import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.keyframes
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.nl.widget.refresh.R

@Composable
fun RefreshIndicator(swipeRefreshState: SwipeRefreshState) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(44.dp),
        contentAlignment = Alignment.Center
    ) {
        if (swipeRefreshState.isRefreshing) {
            val infiniteTransition = rememberInfiniteTransition(label = "")
            val animateAngle by infiniteTransition.animateFloat(
                initialValue = 0f,
                targetValue = 360f,
                animationSpec = infiniteRepeatable(
                    animation = keyframes {
                        durationMillis = 1000
                    },
                    repeatMode = RepeatMode.Restart
                ),
                label = ""
            )
            Image(
                modifier = Modifier
                    .size(24.dp)
                    .graphicsLayer {
                        rotationZ = animateAngle
                    },
                painter = painterResource(
                    id = R.drawable.icon_home_refresh
                ),
                contentDescription = ""
            )
        }

    }
}